#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<vector<int>> pathTarget(TreeNode* root, int target)
    {
        PrevOrder(root, 0, target);
        return _ans;
    }

private:
    void PrevOrder(TreeNode* cur, int sum, int target)
    {
        if (cur == nullptr) {
            return;
        }

        sum += cur->val;
        _v.emplace_back(cur->val);

        if (sum == target && !cur->left && !cur->right) {
            _ans.emplace_back(_v);
        }

        PrevOrder(cur->left, sum, target);
        PrevOrder(cur->right, sum, target);
        _v.pop_back();
    }

    std::vector<int> _v;
    std::vector<std::vector<int>> _ans;
};